#!/bin/bash  
  
# 检查是否以root用户运行  
if [[ $EUID -ne 0 ]]; then  
   echo "该脚本需要以root用户运行，请使用sudo或切换到root用户后重试。"  
   exit 1  
fi  
  
# 检测系统类型  
if [[ -f /etc/os-release ]]; then  
    . /etc/os-release  
    if [[ "$ID" == "ubuntu" ]]; then  
        OS="ubuntu"  
    elif [[ "$ID" == "centos" && "$VERSION_ID" == "7" ]]; then  
        OS="centos7"
        SSH_SERVICE="sshd"  
    else  
        echo "不支持的系统类型，脚本退出。"  
        exit 1  
    fi  
elif [[ -f /etc/redhat-release ]]; then  
    if grep -q "CentOS release 7" /etc/redhat-release; then  
        OS="centos7"  
    else  
        echo "不支持的系统类型，脚本退出。"  
        exit 1  
    fi  
else  
    echo "无法识别系统类型，脚本退出。"  
    exit 1  
fi  
# 询问用户是否需要安装VMware Tools
read -p "是否需要安装VMware Tools？(y/n) " install_vmware_tools

if [[ "$install_vmware_tools" == "y" || "$install_vmware_tools" == "Y" ]]; then
    # 根据系统类型安装 open-vm-tools-desktop
    if [[ "$OS" == "ubuntu" ]]; then
        apt-get update
        apt-get install -y open-vm-tools-desktop
    elif [[ "$OS" == "centos7" ]]; then
        yum install -y epel-release  # 添加 EPEL 仓库以获取 open-vm-tools-desktop
        yum install -y open-vm-tools-desktop
    fi

    echo "VMware Tools (open-vm-tools-desktop) 安装完成。"

else
    echo "无需安装VMware Tools，继续执行后续操作。"
fi

# 根据系统类型安装curl  
if [[ "$OS" == "ubuntu" ]]; then  
    apt-get update  
    apt-get install -y curl  
elif [[ "$OS" == "centos7" ]]; then  
    yum install -y curl  
fi  
  
# 询问用户是否需要更新镜像源  
read -p "是否需要更新镜像源？(y/n) " update_mirror  
if [[ "$update_mirror" == "y" || "$update_mirror" == "Y" ]]; then  
    # 询问用户是否需要添加 --edu 后缀  
    read -p "是否是否使用各大高校的镜像站进行更新(0-默认/1-教育网) " edu_suffix  
    suffix=""  
    if [[ "$edu_suffix" == "1" ]]; then  
        suffix="--edu"  
    fi  
    # 使用curl更新镜像源，根据需要添加 --edu 后缀  
    if [[ -n "$suffix" ]]; then  
        bash <(curl -sSL https://linuxmirrors.cn/main.sh) --edu 
    else  
        bash <(curl -sSL https://linuxmirrors.cn/main.sh)  
    fi  
fi  
 
  
# 询问用户是否需要安装ssh服务  
read -p "是否需要安装SSH服务？(y/n) " install_ssh  
if [[ "$install_ssh" != "y" && "$install_ssh" != "Y" ]]; then  
    echo "无需安装SSH服务，脚本退出。"  
    exit 0  
fi  
  
# 安装openssh-server服务  
if [[ "$OS" == "ubuntu" ]]; then  
    apt-get install -y openssh-server  
elif [[ "$OS" == "centos7" ]]; then  
    yum install -y openssh-server  
fi  
# 重启ssh服务  
if [[ "$OS" == "ubuntu" ]]; then  
    systemctl restart ssh  
elif [[ "$OS" == "centos7" ]]; then  
    systemctl restart sshd  
fi  
  
# 检查ssh服务是否正常运行  
if [[ "$OS" == "ubuntu" ]]; then  
    if systemctl is-active --quiet ssh; then  
        echo "SSH服务已成功运行。"
        echo "当前服务器的IP地址是: $(hostname -I | awk '{print $1}')"    
    else  
        echo "SSH服务未运行，请检查服务状态。"  
        systemctl status ssh  
    fi  
elif [[ "$OS" == "centos7" ]]; then  
    if systemctl is-active --quiet sshd; then  
        echo "SSH服务已成功运行。"  
        echo "当前服务器的IP地址是: $(hostname -I | awk '{print $1}')"  
    else  
        echo "SSH服务未运行，请检查服务状态。"  
        systemctl status sshd  
    fi  
fi  
    
exit 0